﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SemWeb;
using SemWeb.Query;
using SemWeb.Remote;
using System.IO;
using System.Text.RegularExpressions;

namespace QUT.Bio.Web.SemanticCommon {
	#region Class Definitions
	[Serializable]
	public class RdfTag {
		//private static Random rng = new Random();

		public string TagLabelUri = "";
		public string TagLabelName = "";
		public string TagInstanceUri = "";
		public string TagSubjectUri = "";
		public string TagPredicateUri = "";
		public string TagObjectUri = "";
		public string UserIdentifier = "";
		public string TagCreationTime = "";

		#region toRdf
		public Statement[] toRdf() {
			List<Statement> allStatements = new List<Statement>( 15 );
			Statement nextStatement;

			if ( this.TagInstanceUri != null ) {
				nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.rdfType, Entity_Constants.termsInstanceType );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the tag to NTriples because it did not have an instance URI" );
			}

			if ( this.TagLabelUri == null || this.TagLabelUri.Trim().Length == 0 || !Uri.IsWellFormedUriString( this.TagLabelUri, UriKind.Absolute ) ) {
				if ( this.TagLabelName == null || this.TagLabelName.Trim().Length == 0 ) {
					throw new Exception( "Did not convert the tag to NTriples because it did not have an tag label or tag label URI" );
				}

				// we make up a new term instance URI using the namespace defined for this in the manager
				this.TagLabelUri = NS_Constants.manager.GetNamespace( "term" ) + System.Uri.EscapeDataString( this.TagLabelName );
			}

			nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.termsLabelUri, (Entity) ( this.TagLabelUri ) );
			allStatements.Add( nextStatement );

			// avoid creating these definitions if they only gave us a tagLabel URI
			if ( this.TagLabelName != null && this.TagLabelName.Trim().Length > 0 ) {
				nextStatement = new Statement( (Entity) ( this.TagLabelUri ), Entity_Constants.rdfType, Entity_Constants.tagsTagType );
				allStatements.Add( nextStatement );
				nextStatement = new Statement( (Entity) ( this.TagLabelUri ), Entity_Constants.tagsName, (SemWeb.Literal) ( this.TagLabelName ) );
				allStatements.Add( nextStatement );
			}

			if ( this.UserIdentifier != null && Uri.IsWellFormedUriString( this.UserIdentifier, UriKind.Absolute ) ) {
				nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.termsTaggedBy, (Entity) ( this.UserIdentifier ) );
				allStatements.Add( nextStatement );
			}
			else {
				// default to anonymouse
				nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.termsTaggedBy, Entity_Constants.termsAnonymousUser );
				allStatements.Add( nextStatement );
			}

			if ( this.TagSubjectUri == null || this.TagSubjectUri.Trim().Length == 0 || !Uri.IsWellFormedUriString( this.TagSubjectUri, UriKind.Absolute ) ) {
				throw new Exception( "Did not convert the tag to NTriples because it did not have a valid subject URI" );
			}
			else {
				nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.termsSubject, (Entity) ( this.TagSubjectUri ) );
				allStatements.Add( nextStatement );
			}

			if ( Uri.IsWellFormedUriString( this.TagPredicateUri, UriKind.Absolute ) ) {
				nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.termsPredicate, (Entity) ( this.TagPredicateUri ) );
				allStatements.Add( nextStatement );
			}

			if ( Uri.IsWellFormedUriString( this.TagObjectUri, UriKind.Absolute ) ) {
				nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.termsObject, (Entity) ( this.TagObjectUri ) );
				allStatements.Add( nextStatement );
			}

			if ( this.TagCreationTime == null || this.TagCreationTime.Trim().Length == 0 ) {
				this.TagCreationTime = CommonSemanticOperations.getCurrentTimeString();
			}

			nextStatement = new Statement( (Entity) ( this.TagInstanceUri ), Entity_Constants.termsTaggedOn, (SemWeb.Literal) ( this.TagCreationTime ) );
			allStatements.Add( nextStatement );

			return allStatements.ToArray();
		}
		#endregion

		#region toNTriples
		public string toNTriples() {
			return CommonSemanticOperations.writeStatementsToN3String( toRdf() );
		}
		#endregion

		#region clone
		// Clones the valid parts of this RdfTag into a new object
		public RdfTag clone() {
			return clone( false );
		}

		public RdfTag clone( bool newTimestampAndInstanceIdentifierValueNeeded ) {
			RdfTag clonedTag = new RdfTag();

			string correctTime = "";
			if ( newTimestampAndInstanceIdentifierValueNeeded ) {
				correctTime = CommonSemanticOperations.getCurrentTimeString();
			}

			if ( this.TagLabelUri != null && Uri.IsWellFormedUriString( this.TagLabelUri, UriKind.Absolute ) ) {
				clonedTag.TagLabelUri = this.TagLabelUri;
			}

			// Peter_to_do: have to decide on the term URI formatting so they are consistent from the start, and still unique, without requiring unintelligible GUID's if possible
			if ( newTimestampAndInstanceIdentifierValueNeeded ) {
				clonedTag.TagInstanceUri = "http://bio2rdf.org/terminstance:" + System.Uri.EscapeDataString( correctTime ) + "-" + CommonSemanticOperations.rng.Next( 10001, 99999 );
			}
			else if ( this.TagInstanceUri != null && Uri.IsWellFormedUriString( this.TagInstanceUri, UriKind.Absolute ) ) {
				clonedTag.TagInstanceUri = this.TagInstanceUri;
			}

			if ( this.TagSubjectUri != null && Uri.IsWellFormedUriString( this.TagSubjectUri, UriKind.Absolute ) ) {
				clonedTag.TagSubjectUri = this.TagSubjectUri;
			}

			if ( this.TagPredicateUri != null && Uri.IsWellFormedUriString( this.TagPredicateUri, UriKind.Absolute ) ) {
				clonedTag.TagPredicateUri = this.TagPredicateUri;
			}

			if ( this.TagObjectUri != null && Uri.IsWellFormedUriString( this.TagObjectUri, UriKind.Absolute ) ) {
				clonedTag.TagObjectUri = this.TagObjectUri;
			}

			if ( this.UserIdentifier != null && Uri.IsWellFormedUriString( this.UserIdentifier, UriKind.Absolute ) ) {
				clonedTag.UserIdentifier = this.UserIdentifier;
			}

			if ( newTimestampAndInstanceIdentifierValueNeeded ) {
				clonedTag.TagCreationTime = correctTime;
			}
			else if ( this.TagCreationTime != null && this.TagCreationTime.Trim().Length != 0 ) {
				clonedTag.TagCreationTime = this.TagCreationTime;
			}

			if ( this.TagLabelName != null && this.TagLabelName.Trim().Length != 0 ) {
				clonedTag.TagLabelName = this.TagLabelName;
			}

			return clonedTag;
		}
		#endregion

		#region constructors
		public RdfTag() {
		}

		public RdfTag( IEnumerable<Statement> rdfStatements ) {
			foreach ( Statement nextInputStatement in rdfStatements ) {
				if ( nextInputStatement.Predicate.Equals( Entity_Constants.termsTaggedOn ) ) {
					TagCreationTime = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Object );
				}
				else if ( nextInputStatement.Predicate.Equals( Entity_Constants.termsTaggedBy ) ) {
					UserIdentifier = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Object );
				}
				else if ( nextInputStatement.Predicate.Equals( Entity_Constants.termsSubject ) ) {
					TagSubjectUri = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Object );
				}
				else if ( nextInputStatement.Predicate.Equals( Entity_Constants.termsPredicate ) ) {
					TagPredicateUri = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Object );
				}
				else if ( nextInputStatement.Predicate.Equals( Entity_Constants.termsObject ) ) {
					TagObjectUri = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Object );
				}
				else if ( nextInputStatement.Predicate.Equals( Entity_Constants.termsLabelUri ) ) {
					TagLabelUri = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Object );
				}
				else if ( nextInputStatement.Predicate.Equals( Entity_Constants.tagsName ) ) {
					TagLabelName = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Object );
				}
				else if ( nextInputStatement.Predicate.Equals( Entity_Constants.rdfType ) && nextInputStatement.Object.Equals( Entity_Constants.termsInstanceType ) ) {
					TagInstanceUri = SemanticUtility.resolveResourceReferenceString( nextInputStatement.Subject );
				}
			}
		}
		#endregion
	}

	[Serializable]
	public class UriAndLabel {
		public string ItemUri = "";
		public string ItemLabel = "";
	}

	[Serializable]
	public class UriAndCount {
		public string ItemUri = "";
		public int ItemCount = -1;
	}

	[Serializable]
	public class UriAndLabelAndCount {
		public string ItemUri = "";
		public string ItemLabel = "";
		public int ItemCount = -1;
	}

	[Serializable]
	public class SubjectPredicateObject {
		public string SubjectUri = "";
		public string PredicateUri = "";

		public UriAndLabel[] PredicateLabels;

		public string ObjectResource = "";

		public UriAndLabel[] ObjectLabels;

		// use ObjectType to determine whether the object is a URI or a literal
		public string ObjectType = "Literal";

		public SubjectPredicateObject() {
			PredicateLabels = new UriAndLabel[0] { };
			ObjectLabels = new UriAndLabel[0] { };

		}

		public SubjectPredicateObject( Entity newSubject, Entity newPredicate, Resource newObject ) {
			SubjectUri = newSubject.Uri;
			PredicateUri = newPredicate.Uri;

			if ( newObject is SemWeb.Literal ) {
				ObjectType = "Literal";
				ObjectResource = ( newObject as SemWeb.Literal ).Value;
			}
			else {
				ObjectType = "URI";
				ObjectResource = ( newObject as SemWeb.Entity ).Uri;
			}

			PredicateLabels = new UriAndLabel[0] { };
			ObjectLabels = new UriAndLabel[0] { };
		}

		public SubjectPredicateObject( Statement nextStatement )
			: this( nextStatement.Subject, nextStatement.Predicate, nextStatement.Object ) {
		}

		public Statement toRdfStatement() {
			Statement result = new Statement();

			result.Subject = (Entity) SubjectUri;
			result.Predicate = (Entity) PredicateUri;
			if ( ObjectType == "Literal" )
				result.Object = (SemWeb.Literal) ObjectResource;
			else
				result.Object = (Entity) ObjectResource;

			return result;
		}
	}

	[Serializable]
	public class GroupInformation {
		public string GroupUri = "";
		public string GroupName = "";

		public string toNTriples() {
			List<Statement> allStatements = new List<Statement>( 2 );
			Statement nextStatement;

			if ( this.GroupUri != null && Uri.IsWellFormedUriString( this.GroupUri, UriKind.Absolute ) ) {
				nextStatement = new Statement( (Entity) ( this.GroupUri ), Entity_Constants.rdfType, Entity_Constants.termsGroupType );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the group information to NTriples because it did not have a valid group URI" );
			}

			if ( this.GroupName != null && this.GroupName.Trim().Length > 0 ) {
				nextStatement = new Statement( (Entity) ( this.GroupUri ), Entity_Constants.rdfsLabel, (SemWeb.Literal) ( this.GroupName ) );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the group information to NTriples because it did not have a valid group name" );
			}

			return CommonSemanticOperations.writeStatementsToN3String( allStatements.ToArray() );
		}
	}

	[Serializable]
	public class BioPatMLInformation {
		public BioPatMLInformation() {
			myBioPatMLXmlString = "";
			myBioPatMLBio2RdfUri = "";
			myBioPatMLName = "";
			myBioPatMLIdentifier = "";
			myOriginalDatasource = "";
			myExtraAttributes = new List<SubjectPredicateObject>();
		}

		private string myBioPatMLXmlString;

		public string BioPatMLXmlString {
			get {
				return myBioPatMLXmlString;
			}
			set {
				myBioPatMLXmlString = value;
			}
		}

		private string myBioPatMLName;

		public string BioPatMLName {
			get {
				return myBioPatMLName;
			}
			set {
				myBioPatMLName = value;
			}
		}

		private string myBioPatMLIdentifier;

		public string BioPatMLIdentifier {
			get {
				return myBioPatMLIdentifier;
			}
			set {
				myBioPatMLIdentifier = value;
			}
		}

		private string myBioPatMLBio2RdfUri;

		public string BioPatMLBio2RdfUri {
			get {
				return myBioPatMLBio2RdfUri;
			}
			set {
				myBioPatMLBio2RdfUri = value;
			}
		}

		private string myOriginalDatasource;

		public string OriginalDatasource {
			get {
				return myOriginalDatasource;
			}
			set {
				myOriginalDatasource = value;
			}
		}

		private string myDerivedFromUri;

		public string DerivedFromUri {
			get {
				return myDerivedFromUri;
			}
			set {
				myDerivedFromUri = value;
			}
		}


		private List<SubjectPredicateObject> myExtraAttributes;

		public List<SubjectPredicateObject> ExtraAttributes {
			get {
				return myExtraAttributes;
			}
			set {
				myExtraAttributes = value;
			}
		}

		#region toNTriples
		public string toNTriples() {
			return CommonSemanticOperations.writeStatementsToN3String( toRdf() );
		}
		#endregion

		#region toRdf
		public Statement[] toRdf() {
			List<Statement> allStatements = new List<Statement>( 15 );
			Statement nextStatement;

			if ( this.BioPatMLBio2RdfUri != null && Uri.IsWellFormedUriString( this.BioPatMLBio2RdfUri, UriKind.Absolute ) ) {
				nextStatement = new Statement( (Entity) ( this.BioPatMLBio2RdfUri ), Entity_Constants.rdfType, Entity_Constants.biopatmlDefinition );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the biopatml pattern to NTriples because it did not have an identifying URI" );
			}

			if ( this.BioPatMLName != null && this.BioPatMLName.Trim().Length > 0 ) {
				nextStatement = new Statement( (Entity) ( this.BioPatMLBio2RdfUri ), Entity_Constants.rdfsLabel, (SemWeb.Literal) ( this.myBioPatMLName ) );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the biopatml pattern to NTriples because it did not have a name assigned to it" );
			}

			if ( this.BioPatMLIdentifier != null && this.BioPatMLIdentifier.Trim().Length > 0 ) {
				nextStatement = new Statement( (Entity) ( this.BioPatMLBio2RdfUri ), Entity_Constants.dcIdentifier, (SemWeb.Literal) ( this.BioPatMLIdentifier ) );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the biopatml pattern to NTriples because it did not have a name assigned to it" );
			}


			if ( this.BioPatMLXmlString != null && this.BioPatMLXmlString.Trim().Length > 0 ) {
				nextStatement = new Statement( (Entity) ( this.BioPatMLBio2RdfUri ), Entity_Constants.biopatmlXmlSource, (SemWeb.Literal) this.BioPatMLXmlString );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the biopatml pattern to NTriples because it did not have an xml source string" );
			}

			if ( this.OriginalDatasource != null ) {
				nextStatement = new Statement( (Entity) ( this.BioPatMLBio2RdfUri ), Entity_Constants.biopatmlOriginalDatasource, (SemWeb.Literal) this.OriginalDatasource );
				allStatements.Add( nextStatement );
			}
			else {
				throw new Exception( "Did not convert the biopatml pattern to NTriples because it did not have an xml source string" );
			}

			foreach ( SubjectPredicateObject nextExtraAttribute in ExtraAttributes ) {
				allStatements.Add( nextExtraAttribute.toRdfStatement() );
			}

			return allStatements.ToArray();
		}
		#endregion
	}

	[Serializable]
	public class PatternAndTag {
		public string patternUri;
		public string patternName;
		public UriAndLabel[] tags;
	}
	#endregion

	#region CommonSemanticOperations
	/// <summary>
	/// Provides common semantic operations 
	/// </summary>
	public class CommonSemanticOperations {
		public static bool BIO2RDF_IS_IN_MQUTER = true;

		public static Random rng = new Random();

		public static SemanticResultsSink doQuery( string query ) {
			return doQuery( query, System.Configuration.ConfigurationManager.AppSettings["SparqlEndpoint"] );
		}

		public static SemanticResultsSink doQuery( string query, string endpoint ) {
			SemanticResultsSink results = new SemanticResultsSink();

			SparqlHttpSource source = new SparqlHttpSource( endpoint );

			try {
				source.RunSparqlQuery( query, results );
			}
			catch ( Exception ex ) {
				throw ex;
			}
			return results;
		}

		public static Statement[] getRdfFromUrl( string nextUrl ) {
			if ( BIO2RDF_IS_IN_MQUTER && nextUrl.StartsWith( "http://bio2rdf.org/" ) ) {
				nextUrl = "http://bio2rdf.mquter.qut.edu.au" + nextUrl.Substring( "http://bio2rdf.org".Length );
			}

			RdfReader file = RdfReader.LoadFromUri( new Uri( nextUrl ) );

			Store store = new MemoryStore();

			store.Import( file );

			return store.Select( new Statement( null, null, null ) ).ToArray();
		}

		public static string writeStatementsToN3String( Statement[] inputStatements ) {
			MemoryStore store = new MemoryStore( inputStatements );

			StringWriter wr = new StringWriter();

			using ( N3Writer writer = new N3Writer( wr ) ) {
				writer.Format = N3Writer.Formats.NTriples;
				writer.Namespaces.AddFrom( NS_Constants.manager );
				writer.Write( store );
			}

			return wr.ToString();
		}

		public static string getCurrentTimeString() {
			return DateTime.Now.ToUniversalTime().ToString( "yyyy-MM-ddTHH:mm:ssZ" );
		}


		public static bool predicateIsBio2RDFDefault( string predicateUri ) {
			List<string> allDefaultPredicates = new List<string>();

			allDefaultPredicates.Add( Entity_Constants.bio2rdfUrl.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfUrlImage.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfXLinks.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfXProteinLinks.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfXCountLinks.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfXTags.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfUrlData.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfHasSearchResults.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfHasSearchResults.Uri );
			allDefaultPredicates.Add( Entity_Constants.bio2rdfRawRdf.Uri );

			foreach ( string nextString in allDefaultPredicates ) {
				if ( nextString == predicateUri )
					return true;
			}

			return false;
		}

		public static string transformBio2RDFUriForQuery( string standardBio2rdfUri, string query ) {
			string nsAndId = standardBio2rdfUri.Substring( "http://bio2rdf.org/".Length );

			return SemanticUtility.createPercentEncodeUrlWithPrefix( nsAndId, "http://bio2rdf.org/" + query + "/" );
		}
	}
	#endregion

	#region SemanticResultsSink
	public class SemanticResultsSink : QueryResultSink {
		public List<VariableBindings> resultsList = new List<VariableBindings>();

		public override bool Add( VariableBindings result ) {
			resultsList.Add( result );

			return true;
		}
	}
	#endregion

	#region SemanticUtility
	public class SemanticUtility {
		public static string resolveResourceReferenceString( Resource givenResource ) {
			try {
				if ( givenResource is Entity ) {
					return ( givenResource as Entity ).Uri;
				}
				else {
					return ( givenResource as SemWeb.Literal ).Value;
				}
			}
			catch ( Exception objectEncodeException ) {
				throw objectEncodeException;
			}
		}

		//public static bool loggedIn()
		//{
		//   this.Context.Request.Cookies.
		//   return State.AuthenticationResponse != null && State.AuthenticationResponse.Status == DotNetOpenAuth.OpenId.RelyingParty.AuthenticationStatus.Authenticated;
		//}

		public static string encodeRdfLiteral( string inputString ) {
			string returnString = inputString;

			returnString = returnString.Replace( "\\", "\\\\" ).Replace( '"', '\"' ).Replace( "\n", " " );

			return returnString;
		}

		public static string createPercentEncodeUrlWithPrefix( string inputTerm, string prefix ) {
			return prefix + System.Uri.EscapeDataString( inputTerm );
		}

	}
	#endregion

	#region Entity_Constants
	public static class Entity_Constants {
		#region public uri entities
		// the following should match literals...
		public static Entity rdfType = (Entity) ( NS_Constants.manager.GetNamespace( "rdf" ) + "type" ); // http://www.w3.org/1999/02/22-rdf-syntax-ns#type  
		public static Entity rdfsLabel = (Entity) ( NS_Constants.manager.GetNamespace( "rdfs" ) + "label" ); // http://www.w3.org/2000/01/rdf-schema#label
		public static Entity rdfsComment = (Entity) ( NS_Constants.manager.GetNamespace( "rdfs" ) + "comment" ); // http://www.w3.org/2000/01/rdf-schema#comment
		public static Entity dcTitle = (Entity) ( NS_Constants.manager.GetNamespace( "dc" ) + "title" ); // http://purl.org/dc/elements/1.1/title
		public static Entity dcCreator = (Entity) ( NS_Constants.manager.GetNamespace( "dc" ) + "creator" ); // http://purl.org/dc/elements/1.1/creator
		public static Entity dcIdentifier = (Entity) ( NS_Constants.manager.GetNamespace( "dc" ) + "identifier" ); // http://purl.org/dc/elements/1.1/identifier
		public static Entity foafName = (Entity) ( NS_Constants.manager.GetNamespace( "foaf" ) + "name" ); // http://xmlns.com/foaf/0.1/name
		public static Entity bio2rdfUrl = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "url" ); // http://bio2rdf.org/bio2rdf#url
		public static Entity bio2rdfUrlImage = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "urlImage" ); // http://bio2rdf.org/bio2rdf#urlImage

		// the following should match resources...
		public static Entity owlSameAs = (Entity) ( NS_Constants.manager.GetNamespace( "owl" ) + "sameAs" ); // http://www.w3.org/2002/07/owl#sameAs
		public static Entity rdfsSeeAlso = (Entity) ( NS_Constants.manager.GetNamespace( "rdfs" ) + "seeAlso" ); // http://www.w3.org/2000/01/rdf-schema#seeAlso
		public static Entity omimXLocus = (Entity) ( NS_Constants.manager.GetNamespace( "omim" ) + "xLocus" );// http://bio2rdf.org/ns/omim#xLocus
		public static Entity geneIdXDB = (Entity) ( NS_Constants.manager.GetNamespace( "geneid" ) + "xDB" ); // http://bio2rdf.org/ns/geneid#xDB
		public static Entity geneIdGeneType = (Entity) ( NS_Constants.manager.GetNamespace( "geneid" ) + "Gene" ); //http://bio2rdf.org/ns/geneid#Gene
		public static Entity bio2rdfArticle = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xArticle" ); // http://bio2rdf.org/ns/bio2rdf#xArticle
		public static Entity bio2rdfXRef = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xRef" ); // http://bio2rdf.org/ns/bio2rdf#xRef  
		public static Entity bio2rdfXGenBank = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xGenBank" ); // http://bio2rdf.org/ns/bio2rdf#xGenBank  
		public static Entity bio2rdfXGI = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xGI" ); // http://bio2rdf.org/ns/bio2rdf#xGI  
		public static Entity bio2rdfHgncAccession = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf_hgnc" ) + "accessionNumbers" ); // http://bio2rdf.org/ns/hgnc#accessionNumbers
		public static Entity bio2rdfHgncEntrezGeneId = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf_hgnc" ) + "entrezGene_id" ); // http://bio2rdf.org/ns/hgnc#entrezGene_id
		public static Entity bio2rdfHgncEntrezGeneIdMapped = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf_hgnc" ) + "entrezGene_id_mapped_data" ); // http://bio2rdf.org/ns/hgnc#entrezGene_id_mapped_data
		public static Entity bio2rdfHgncPubMed = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf_hgnc" ) + "pubMed_id" ); // http://bio2rdf.org/ns/hgnc#pubMed_id

		public static Entity termsInstanceType = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "TermInstance" );
		public static Entity termsGroupType = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "Group" );

		public static Entity tagsTagType = (Entity) ( NS_Constants.manager.GetNamespace( "tags" ) + "Tag" );
		public static Entity tagsName = (Entity) ( NS_Constants.manager.GetNamespace( "tags" ) + "name" );

		public static Entity termsTaggedOn = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "taggedOn" );
		public static Entity termsTaggedBy = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "taggedBy" );
		public static Entity termsSubject = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "subject" );
		public static Entity termsPredicate = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "predicate" );
		public static Entity termsObject = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "object" );
		public static Entity termsLabelUri = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "term" );
		public static Entity termsLabelName = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "termName" );
		public static Entity termsAnonymousUser = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "AnonymousUser" );
		public static Entity termsAnonymousGroup = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "AnonymousGroup" );
		public static Entity termsMemberOf = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "memberOf" );
		public static Entity termsUsefulForPredicate = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "UsefulForPredicate" );
		public static Entity termsUsefulForType = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "UsefulForType" );

		public static Entity termsUsefulForTaggingType = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "UsefulForTaggingType" ); // http://purl.org/net/terms#UsefulForTaggingType

		public static Entity biopatmlDefinition = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf_biopatml" ) + "Definition" ); // http://bio2rdf.org/ns/biopatml#Definition
		public static Entity biopatmlOriginalDatasource = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf_biopatml" ) + "OriginalDatasource" ); // http://bio2rdf.org/ns/biopatml#OriginalDatasource
		public static Entity biopatmlXmlSource = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf_biopatml" ) + "XmlSource" ); // http://bio2rdf.org/ns/biopatml#XmlSource

		public static Entity termsGraphIndexUri = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "GraphIndex" );

		public static Entity termsSparqlGraphUri = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "TermsSparqlGraph" ); // http://purl.org/net/terms#TermsSparqlGraph
		public static Entity termsGroupMemberGraphUri = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "GroupMemberList" ); // http://purl.org/net/terms#GroupMemberList
		public static Entity termsGroupGraphUri = (Entity) ( NS_Constants.manager.GetNamespace( "terms" ) + "GroupList" ); // http://purl.org/net/terms#GroupList

		public static Entity bio2rdfXLinks = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xLinks" ); // http://bio2rdf.org/ns/bio2rdf#xLinks  
		public static Entity bio2rdfXCountLinks = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xCountLinks" ); // http://bio2rdf.org/ns/bio2rdf#xCountLinks  
		public static Entity bio2rdfXTags = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xTags" ); // http://bio2rdf.org/ns/bio2rdf#xTags  
		public static Entity bio2rdfUrlData = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "urlData" ); // http://bio2rdf.org/ns/bio2rdf#urlData  
		public static Entity bio2rdfHasSearchResults = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "hasSearchResults" ); // http://bio2rdf.org/ns/bio2rdf#hasSearchResults  
		public static Entity bio2rdfXProteinLinks = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "xProteinLinks" ); // http://bio2rdf.org/ns/bio2rdf#xProteinLinks  
		public static Entity bio2rdfRawRdf = (Entity) ( NS_Constants.manager.GetNamespace( "bio2rdf" ) + "rawRdf" ); // http://bio2rdf.org/ns/bio2rdf#rawRdf  

		#endregion
	}
	#endregion

	#region NS_Constants
	public static class NS_Constants {
		public static SemWeb.NamespaceManager manager;

		static NS_Constants() {
			manager = new NamespaceManager();

			manager.AddNamespace( "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf" );
			manager.AddNamespace( "http://xmlns.com/foaf/0.1/", "foaf" );
			manager.AddNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
			manager.AddNamespace( "http://www.w3.org/2002/07/owl#", "owl" );
			manager.AddNamespace( "http://www.w3.org/2000/01/rdf-schema#", "rdfs" );
			manager.AddNamespace( "http://bio2rdf.org/ns/bio2rdf#", "bio2rdf" );
			manager.AddNamespace( "http://bio2rdf.org/ns/kegg#", "bio2rdf_kegg" );
			manager.AddNamespace( "http://bio2rdf.org/ns/go#", "bio2rdf_go" );
			manager.AddNamespace( "http://bio2rdf.org/ns/bibtex#", "bio2rdf_bibtex" );
			manager.AddNamespace( "http://bio2rdf.org/ns/omim#", "bio2rdf_omim" );
			manager.AddNamespace( "http://bio2rdf.org/ns/geneid#", "bio2rdf_geneid" );
			manager.AddNamespace( "http://bio2rdf.org/ns/pubmed#", "bio2rdf_pubmed" );
			manager.AddNamespace( "http://bio2rdf.org/ns/hgnc#", "bio2rdf_hgnc" );
			manager.AddNamespace( "http://bio2rdf.org/ns/biopatml#", "bio2rdf_biopatml" );
			manager.AddNamespace( "http://purl.org/net/terms#", "terms" );
			manager.AddNamespace( "http://bio2rdf.org/term:", "term" );
			manager.AddNamespace( "http://bio2rdf.org/terminstance:", "terminstance" );
			manager.AddNamespace( "http://www.holygoat.co.uk/owl/redwood/0.1/tags/", "tags" );
		}
	}
	#endregion

}